package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @RequiresPermissions(value={"department:view","部门视图"},logical = Logical.OR)
    @RequestMapping("view")
    public String view() {
        return "department/department";
    }

    @RequiresPermissions(value={"department:query","部门列表"},logical = Logical.OR)
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(QueryObject qo) {
        return departmentService.query(qo);
    }

    @RequiresPermissions(value={"department:selectAll","部门全选"},logical = Logical.OR)
    @RequestMapping("selectAll")
    @ResponseBody
    public List<Department> selectAll() {
        return departmentService.selectAll();
    }

    @RequiresPermissions(value={"department:saveOrUpdate","部门编辑/新增"},logical = Logical.OR)
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department entity) {
        try {
            if (entity.getId() == null) {
                departmentService.insert(entity);
            } else {
                departmentService.updateByPrimaryKey(entity);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }
    @RequiresPermissions(value={"department:changState","部门状态"},logical = Logical.OR)
    @RequestMapping("changState")
    @ResponseBody
    public JsonResult changState(Long id) {
        try {
            departmentService.changState(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }


}
